﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using OnlineSoccerManager.Classes;
using OnlineSoccerManager.Helper;

namespace OnlineSoccerManager
{
    public class MainViewModel : BaseNotifyPropertyChanged
    {
        private WebClient client;
        private string password;
        private string userName;
        private bool isBusy;
        private bool isRefreshing;
        private bool hasError;
        private string lastError;
        private System.Timers.Timer errorTimer = new System.Timers.Timer();
        private WindowState currentState = WindowState.LOGIN;
        private Translator translator;
        private Player selectedPlayer;

        private List<Player> profiKader = null;

        public MainViewModel()
        {
            this.client = new WebClient();
            this.client.BaseUrl = "http://www.ksm-soccer.de/";

            this.LoginCommand = new RelayCommand(this.LoginCommandExecute);

            this.errorTimer.Elapsed += (s, a) =>
            {
                this.errorTimer.Stop();
                this.HasError = false;
            };
            this.errorTimer.Interval = 3000;
            this.translator = Translator.Instance;
        }

        public Player SelectedPlayer
        {
            get
            {
                return this.selectedPlayer;
            }
            set
            {
                this.selectedPlayer = value;
                this.OnPropertyChanged("SelectedPlayer");
            }

        }

        public Translator Translator
        {
            get
            {
                return this.translator;
            }
        }

        public string Password {
            get
            {
                return this.password;
            }
            set
            {
                this.password = value;
                this.OnPropertyChanged("Password");
            }
        }


        public string UserName
        {
            get
            {
                return this.userName;
            }
            set
            {
                this.userName = value;
                this.OnPropertyChanged("UserName");
            }
        }

        public WindowState CurrentState {
            get
            {
                return this.currentState;
            }
            set
            {
                this.currentState = value;
                this.OnPropertyChanged("CurrentState");
            }
        }

        public bool IsBusy
        {
            get
            {
                return this.isBusy;
            }
            set
            {
                this.isBusy = value;
                this.OnPropertyChanged("IsBusy");
            }
        }

        public bool IsRefreshing
        {
            get
            {
                return this.isRefreshing;
            }
            set
            {
                this.isRefreshing = value;
                this.OnPropertyChanged("IsRefreshing");
            }
        }
        public bool HasError
        {
            get
            {
                return this.hasError;
            }
            set
            {
                this.hasError = value;
                if (this.hasError)
                    this.errorTimer.Start();
                this.OnPropertyChanged("HasError");
            }
        }

        public string LastError
        {
            get
            {
                return this.lastError;
            }
            set
            {
                this.lastError = value;
                if (value != null)
                    this.HasError = true;
                this.OnPropertyChanged("LastError");
            }
        }

        public List<Player> ProfiKader
        {
            get
            {
                return this.profiKader;
            }
            set
            {
                if (this.profiKader == value)
                    return;

                this.profiKader = value;
                this.OnPropertyChanged("ProfiKader");
            }
        }

        #region Commands

        public RelayCommand LoginCommand { get; set; }

        private void LoginCommandExecute(object param)
        {
            // block GUI till response
            this.IsBusy = true;

            // do work in another Task to avoid freezing gui
            Task.Factory.StartNew(() => {
                // try to log in
                bool loggedin = this.client.Login(this.UserName, this.Password);

                // if logged in load inital Data
                if (loggedin)
                    this.Refresh();

                // gui stuff must be performed in gui thread so invoke that task...
                this.Password = null;

                // if something went wrong show error
                if (!loggedin)
                    this.LastError = "Login fehlgeschlagen: Passwort oder Benutzername falsch!";
                else
                    this.CurrentState = WindowState.HOME;

                this.IsBusy = false;

            });
        }

        private void Refresh()
        {
            this.IsRefreshing = true;

            this.ProfiKader = this.client.GetTeam();
            // refresh all other stuff ....

            this.IsRefreshing = false;
        }

        #endregion


    }
}
